angular.module('versionAddApp', []).controller( 'versionAddCtr',
	function($scope, $http) {
	
		$scope.type1="index_districtIndex";  //复选框默认值0，没有选中的值
		$scope.type2='0';  //复选框默认值0，没有选中的值
		$scope.type3='0';  //复选框默认值0，没有选中的值
		$scope.type4='0';
		$scope.type5='0';
		$scope.type6='0';
		$scope.type7='0';
		$scope.type8='0';
		$scope.type9='0';
		
		$scope.$watch('type1',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type2',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type3',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type4',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type5',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type6',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type7',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type8',
				function() {
					$scope.changePage();
				});
		$scope.$watch('type9',
				function() {
					$scope.changePage();
				});
		$scope.changePage = function() {
			$scope.versionPass = $scope.type1+"_"+$scope.type2+"_"+$scope.type3+"_"+$scope.type4+"_"+$scope.type5+"_"+$scope.type6+"_"+$scope.type7+"_"+$scope.type8+"_"+$scope.type9;
		}
			
		// 点击保存按钮，新增一条数据
		$scope.saveVersion = function() {
			// form表单的验证 数据不对，return false
			var formValid = $scope.versionForm.$valid;
			if (!$scope.versionName) {
				toastr.warning("版本名称不可为空！");
				formValid = false;
				return false;
			}
			if (!$scope.organizeId) {
				toastr.warning("组织不可为空！");
				formValid = false;
				return false;
			}
			if (formValid) { // 表单通过验证后提交
				checkname(); // 校验版本名称是否重复
			}
		};

		function checkname() {
			var result = true;
			$http({
				url : basePath+'/system/checkVersionName',
				method : 'POST',
				params : {
					'versionName' : $scope.versionName
				}
			}).success(function(data, header, config, status) {
				if (data.length == '0') { // 没有重复的属性名称
					submitData();
				} else {
					toastr.warning("版本名称重复！");
					return;
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				result = false;
			});
		}
		
		function submitData(){
			var form = $("form[name=versionForm");  
			var options  = {
				url:basePath+'/system/saveVersion',
                type:'post',  
                data:{
					'versionName' : $scope.versionName,
					'province'   :  $scope.province,
					'city' : $scope.city,
					'county' : $scope.county,
					'versionPass':$scope.versionPass,
					'versionDesc':$scope.versionDesc,
					'versionState':$scope.versionState,
					'organizeId':$scope.organizeId
                },  
                success:function(data){  
                   if(data){ 
                           toastr.success('操作数据成功!'); 
                           setTimeout(function() { 
                        	   location.href = basePath+'/system/versionApply';
                           }, 500); 
                     }else{ 
                          toastr.error('操作数据失败!'); 
                          setTimeout(function() {
          				}, 500);
                     } 

            	}  
        };  
        form.ajaxSubmit(options);  
	 }
		
		//  点击取消按钮，跳转到查询页面
		$scope.backHis = function() {
			location.href = basePath+'/system/versionApply';
		};
});

$(document).ready(function() { 
	
    $('#province').change(function(){ 
    	
    	
		var districtCode=$(this).children('option:selected').val(); 
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/district/district",
			data : {parentCode:districtCode},
			success : function(data) {
				if(data.length>0){
					$("#city").empty();
					$("#city").append("<option value=''>请选择</option>");
					$("#county").empty();
					$("#county").append("<option value=''>请选择</option>");
					for(var i=0;i<data.length;i++){
						$("#city").append("<option value="+data[i].code+">"+data[i].name+"</option>");
					}
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		 });
		findOrg(districtCode);
	}); 
	
	$('#city').change(function(){ 
		var districtCode=$(this).children('option:selected').val(); 
		
		$("#county").empty();
		$("#county").append("<option value=''>请选择</option>");
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/district/district",
			data : {parentCode:districtCode},
			success : function(data) {
				if(data.length>0){
					for(var i=0;i<data.length;i++){
						$("#county").append("<option value="+data[i].code+">"+data[i].name+"</option>");
					}
					
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		 }); 
		findOrg(districtCode);
	}); 
	
	$('#county').change(function(){ 
		var districtCode=$(this).children('option:selected').val(); 
		findOrg(districtCode);
	});
	
	//查询组织函数
	function findOrg(districtCode){
		$("#organizeId").empty();
		$("#organizeId").append("<option value=''>请选择</option>");
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/user/getOrg",
			data : {districtCode:districtCode},
			success : function(data) {
				if(data.length>0){
					for(var i=0;i<data.length;i++){
						$("#organizeId").append("<option value="+data[i].id+">"+data[i].organizeName+"</option>");
					}
				}
			}
		});
	}
	
    });